package net.lab1024.sa.admin.module.users.service;

import java.util.List;
import net.lab1024.sa.admin.module.users.dao.UsersDao;
import net.lab1024.sa.admin.module.users.domain.entity.UsersEntity;
import net.lab1024.sa.admin.module.users.domain.form.UsersAddForm;
import net.lab1024.sa.admin.module.users.domain.form.UsersQueryForm;
import net.lab1024.sa.admin.module.users.domain.form.UsersUpdateForm;
import net.lab1024.sa.admin.module.users.domain.vo.UsersVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 实训室管理系统 Service
 *
 * @Author 王俊权
 * @Date 2024-11-06 10:32:24
 * @Copyright 🐂🐎bloc
 */

@Service
public class UsersService {

    @Resource
    private UsersDao usersDao;

    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<UsersVO> queryPage(UsersQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<UsersVO> list = usersDao.queryPage(page, queryForm);
        PageResult<UsersVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(UsersAddForm addForm) {
        UsersEntity usersEntity = SmartBeanUtil.copy(addForm, UsersEntity.class);
        usersDao.insert(usersEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(UsersUpdateForm updateForm) {
        UsersEntity usersEntity = SmartBeanUtil.copy(updateForm, UsersEntity.class);
        usersDao.updateById(usersEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Integer> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        usersDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Integer id) {
        if (null == id){
            return ResponseDTO.ok();
        }

        usersDao.deleteById(id);
        return ResponseDTO.ok();
    }
}
